home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
CLMUTEX.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-15
|
11KB
|
368 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: clmutex.cpp
// Title: C++ Class Libraries
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
// This module contains code for the class CL_MUTEX.
//
// The code in this module may be written in C++ or C.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
//
//----------------------------------------------------------------------------
#include <class.hpp>
//----------------------------------------------------------------------------
// Description: Default constructor
// Parameters:
// Returns:
//----------------------------------------------------------------------------
FN_M CL_MUTEX::CL_MUTEX()
{
CL_MUTEX::Initialize(CL_INIT_CLASS);
}
//----------------------------------------------------------------------------
// Description: Copy constructor
// Parameters: rccl_mutex Reference to object to copy.
// Returns:
//----------------------------------------------------------------------------
FN_M CL_MUTEX::CL_MUTEX(RCCL_MUTEX rccl_mutex)
{
CL_MUTEX::Initialize(CL_INIT_CLASS);
*this = rccl_mutex;
}
//----------------------------------------------------------------------------
// Description: Destructor
// Parameters:
// Returns:
//----------------------------------------------------------------------------
FN_M CL_MUTEX::~CL_MUTEX()
{
CL_MUTEX::Destroy(FALSE);
}
//----------------------------------------------------------------------------
// Description: Destroy object. Free any resources used by object.
// Normally called by destructor.
// Should allow multiple calls from various classes.
// Parameters: fDestroyAll Destroy parents also?
// Default is TRUE.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_M CL_MUTEX::Destroy(BOOL fDestroyAll)
{
#if OS_OS2
if (fCreate)
{
DosCloseMutexSem(hmtx);
fCreate = FALSE;
}
#endif
CL_MUTEX::Initialize(CL_INIT_CLASS_VARS);
if (fDestroyAll) // Destroy parent.
CL_MUTEX_PARENT::Destroy(fDestroyAll);
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Initialize object.
// Normally called by constructor.
// Should allow multiple calls from various classes.
// Parameters: sInit Initialization code. May be one of the following:
// CL_INIT_CLASS Reset class variables and
// and dynamic allocations for
// this class only.
// CL_INIT_CLASS_VARS Reset class variables for
// this class only.
// CL_INIT_VARS Reset class variables for
// this class only.
// CL_INIT_ALL Initialize class and all
// parent class, including
// dynamic memory allocation.
// Default is CL_INIT_ALL
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_M CL_MUTEX::Initialize(SHORT sInit)
{
if (sInit == CL_INIT_VARS || sInit == CL_INIT_ALL)
CL_MUTEX_PARENT::Initialize(sInit);
#if OS_OS2
cLock = 0;
tid = 0;
fCreate = FALSE;
#endif
if (sInit == CL_INIT_CLASS_VARS || sInit == CL_INIT_VARS)
return TRUE;
#if OS_OS2
if (DosCreateMutexSem(NULL, &hmtx, 0, 0) != SUCCESS)
SetError();
else
fCreate = TRUE;
#endif
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Check if object is in error state.
// IsValid() && IsError() MUST NOT BE DEPENDENT ON ONE ANOTHER.
// Parameters:
// Returns: TRUE if in error state.
//----------------------------------------------------------------------------
BOOL FN_M CL_MUTEX::IsError() const
{
return CL_MUTEX_PARENT::IsError();
}
//----------------------------------------------------------------------------
// Description: Check if object is valid
// IsValid() && IsError() MUST NOT BE DEPENDENT ON ONE ANOTHER.
// Parameters:
// Returns: TRUE if valid
//----------------------------------------------------------------------------
BOOL FN_M CL_MUTEX::IsValid() const
{
return CL_MUTEX_PARENT::IsValid();
}
//----------------------------------------------------------------------------
// Description: Assignment operator
// NOTE: Don't copy object into self
// Parameters: rccl_mutex Reference to right value.
// Returns: Reference to new object.
//----------------------------------------------------------------------------
RCCL_MUTEX FN_M CL_MUTEX::operator=(RCCL_MUTEX rccl_mutex)
{
if (this != &rccl_mutex)
{
Invalid("CL_MUTEX::operator=");
}
return (RCCL_MUTEX)*this;
}
//----------------------------------------------------------------------------
// Description: Release a mutual exclusion semapore
// Parameters:
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_M CL_MUTEX::Release()
{
if (IsError())
return FALSE;
#if OS_OS2
Assert(tid == THREADID);
if (!fCreate)
return FALSE;
if (cLock)
cLock--;
if (!cLock)
{
if (DosReleaseMutexSem(hmtx) != SUCCESS)
{
SetError();
return FALSE;
}
tid = 0;
}
#endif
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Request mutual exclusion semaphore
// Parameters: lTimeOut Time out period. -1L == Indefinite
// Default is -1L.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_M CL_MUTEX::Request(LONG lTimeOut)
{
if (IsError())
return FALSE;
#if OS_OS2
if (!fCreate)
return FALSE;
if (tid == THREADID)
{
cLock++;
return TRUE;
}
if (DosRequestMutexSem(hmtx, lTimeOut) != SUCCESS)
{
SetError();
return FALSE;
}
tid = THREADID;
cLock = 1;
#else
NOTUSED(lTimeOut);
#endif
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Retrieve object from persistent storage
// Parameters: pcsz Name of object.
// pcszSub Sub-name of object.
// The first character of the name should be '~'.
// If NULL, no sub name is available.
// Default is NULL
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_M CL_MUTEX::Retrieve(PCSZ pcsz, PCSZ pcszSub)
{
NOTUSED(pcsz);
NOTUSED(pcszSub);
Invalid("CL_MUTEX::Retrieve");
return FALSE;
}
//----------------------------------------------------------------------------
// Description: Store object to persistent storage
// Parameters: pcsz Name of object.
// pcszSub Sub-name of object.
// The first character of the name should be '~'.
// If NULL, no sub name is available.
// Default is NULL
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_M CL_MUTEX::Store(PCSZ pcsz, PCSZ pcszSub)
{
NOTUSED(pcsz);
NOTUSED(pcszSub);
Invalid("CL_MUTEX::Store");
return FALSE;
}
//----------------------------------------------------------------------------
// Description: Print object value to debugging output.
// Parameters: pccl_mutex Pointer to dynamic object.
// If NULL, static data elements are printed.
// Default is NULL.
// pcsz Name of object.
// If NULL, no name is displayed.
// Default is NULL.
// cLevel Display level.
// Default is zero.
// Returns:
//----------------------------------------------------------------------------
#if COMPILE_DEBUG
VOID FN_M CL_MUTEX::Print(PCCL_MUTEX pccl_mutex, PCSZ pcsz, SIZET cLevel)
{
#if COMPILE_TEST
OutputL(cLevel, "CL_MUTEX%s%s", (pcsz?"::":""), (pcsz?pcsz:""));
cLevel++;
if (pccl_mutex)
{
Output(" <%p>\n", pccl_mutex);
if(!pccl_mutex->IsError())
{
#if OS_OS2
OutputL(cLevel, "tid = %d\n", pccl_mutex->tid);
OutputL(cLevel, "cLock = %u\n", pccl_mutex->cLock);
OutputL(cLevel, "fCreate = %u\n", pccl_mutex->fCreate);
#endif
}
}
else
Output(" <NULL>\n");
CL_MUTEX_PARENT::Print((CL_MUTEX_PARENT _FAR_ *)pccl_mutex, pcsz, cLevel);
return ;
#else
NOTUSED(cLevel);
NOTUSED(pccl_mutex);
NOTUSED(pcsz);
return ;
#endif
}
#endif
//----------------------------------------------------------------------------
// Description: Run standard test suite on object.
// Parameters: sTest Test to run.
// If 0, run default tests.
// Default is 0.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
#if COMPILE_DEBUG
BOOL FN_M CL_MUTEX::Test(SHORT sTest)
{
#if COMPILE_TEST
if (sTest == 1) // Test 1 is always a test of storage
{
CL_MUTEX cl_mutex;
cl_mutex.Store("CL_MUTEX");
cl_mutex.Retrieve("CL_MUTEX");
CL_MUTEX::Print(&cl_mutex);
}
CL_MUTEX cl_mutex;
cl_mutex.Request();
Print(&cl_mutex);
cl_mutex.Release();
Print(&cl_mutex);
return TRUE;
#else
NOTUSED(sTest);
return TRUE;
#endif
}
#endif
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------